import axios from "axios";
import { useState, useEffect } from "react";
import { message } from "antd";

const useAxios = options => {
  const [isLoading, setIsLoading] = useState(true);
  const [response, setResponse] = useState(null);
  const [error, setError] = useState(null);

  useEffect(() => {
    const fetchData = async () => {
      axios(options)
        .then(
          res => {
            const data = res.data;
            if (data.code == 0 || data.code == 200) {
              setResponse(data);
              setIsLoading(false);
            } else {
              throw new Error(data.msg || "后端接口异常");
            }
          },
          err => {
            throw new Error("后端接口异常");
          }
        )
        .catch(err => {
          message.error("后端接口异常");
        })
        .finally(() => {});
    };

    fetchData();
  }, []);
  return [response, error, isLoading];
};

export default useAxios;
